package com.wql.aop01.service.aspect;

import com.wql.aop01.service.annotion.WqlAnnotion;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wql
 * @version 1.0
 * @date 2022/11/11 21:08
 */
@Component
@Aspect
public class PermissionAdvice {
    @Pointcut("@annotation(com.wql.aop01.service.annotion.WqlAnnotion)")
    public void  wqlpointcut(){};

    private List<String> permissions=new ArrayList<String>();
    public PermissionAdvice(){
        permissions.add("list");
        permissions.add("delete");
        permissions.add("update");
    }
    @Around("wqlpointcut()")
    public Object before(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature =(MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        WqlAnnotion annotation = method.getAnnotation(WqlAnnotion.class);
        if (annotation!=null){
            String value = annotation.value();
            if (!permissions.contains(value)){
                return "抱歉，您没有权限";
            }
        }
        return joinPoint.proceed();
    }
}
